package com.dinetech.ejecutor;

import java.util.ArrayList;

import com.dinetech.dao.SpinnerVariosAdapter;
import com.dinetech.dao.WSEvento;
import com.dinetech.entidades.Eventos;
import com.dinetech.entidades.Personal;
import com.dinetech.entidades.Session;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Evento_Ingreso extends Activity {
	EditText CantMacho;
	EditText CantHembra;
	EditText CantGazapo;
	EditText Comentario;
	Button BtnActualizar;
	TextView lblPosa;
	Spinner cbxResponsable;
	Spinner cbxUbicacion;
	Session s = Session.getInstance();
	Personal per =Personal.getInstance();
	int idPersonal,IdEvento,idUbicacion;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.evento_ingreso_externo);
		
		Bundle bundle = getIntent().getExtras();
		IdEvento = bundle.getInt("IdEvento");

		//Llamo al hilo que ejecuta la WebServices
		new TaskListado().execute(IdEvento);
	}
	
	private class TaskListado extends AsyncTask<Integer, Void, ArrayList<Eventos>> {

		
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			

		}

		@Override
		protected ArrayList<Eventos> doInBackground(Integer... arg0) {
			// TODO Auto-generated method stub
			//Looper.prepare();

	    	WSEvento WSEvento= new WSEvento();
			ArrayList<Eventos> lstEventos = new ArrayList<Eventos>();
			int IdEvento = Integer.parseInt(arg0[0].toString());
			lstEventos = WSEvento.WSListarEventosRegistrados(IdEvento);
	    	
	    	return lstEventos;

		}

		@Override
		protected void onPostExecute(ArrayList<Eventos> lstEventos) {
			// TODO Auto-generated method stub
			super.onPostExecute(lstEventos);
			BtnActualizar = (Button)findViewById(R.id.btnIngresoEx);
			CantMacho = (EditText)findViewById(R.id.txtCantMachosIE);
			CantHembra = (EditText)findViewById(R.id.txtCantHembrasIE);
			CantGazapo= (EditText)findViewById(R.id.txtCantGazaposIE);
			Comentario = (EditText)findViewById(R.id.txtComentarioIE);
			TextView lblPosa;
			
			BtnActualizar.setText(R.string.btn_Actualizar);
			//CantMacho.setText(lstEventos.get(0).getCantMachos());
			CantHembra.setText(String.valueOf(lstEventos.get(0).getCantHembras()));
			CantMacho.setText(String.valueOf(lstEventos.get(0).getCantMachos()));
			CantGazapo.setText(String.valueOf(lstEventos.get(0).getCantGazapos()));
			Comentario.setText(lstEventos.get(0).getComentario());
			cbxUbicacion = (Spinner)findViewById(R.id.cbxPozaIE);
			cbxResponsable =(Spinner)findViewById(R.id.cbxResponsableIE);
			lblPosa = (TextView)findViewById(R.id.lblFechaC);
			
			cbxUbicacion.setEnabled(false);
			idUbicacion = lstEventos.get(0).getIdUbicacionOrigen();
			lblPosa.setText("Poza / Jaula : "+ lstEventos.get(0).getIdClientUbibcacion());
			//Lista Cbx de Responsable
			listarSpinner();
			
			BtnActualizar.setOnClickListener(new ButtonEvento());
		}

	}
	
private void listarSpinner(){
		
		SpinnerVariosAdapter adapterR = new SpinnerVariosAdapter(Evento_Ingreso.this, s.getLISTA_RESPONSABLE());
		cbxResponsable.setAdapter(adapterR);
		cbxResponsable.setOnItemSelectedListener(new SpinnerListenerR());
		
		for(int i=0; i <  adapterR.getCount(); i++){
			int idRA=(int)(long)adapterR.getItemId(i) ;
			idPersonal = per.getIdPersonal();
			if( idRA == idPersonal){
				cbxResponsable.setSelection(i);					
			}	        
		}
		
	}
	public class SpinnerListenerR implements OnItemSelectedListener {
	      
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        
            int convert = Integer.parseInt( Long.toString(id));
            idPersonal = convert;	            	         
        }

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
			
		}
	}
	
	private class ButtonEvento implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new TaskActualizarEvento().execute();	
		}
	
	}
	
	public class TaskActualizarEvento  extends AsyncTask<String,Void,String>{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			
			WSEvento we = new WSEvento();
			String Msg = we.ActualizarEvento(IdEvento,"01", "12", "0", "0", s.obtenerFechaActual(), String.valueOf(idUbicacion), "0", Integer.parseInt(CantMacho.getText().toString()), Integer.parseInt(CantHembra.getText().toString()), Integer.parseInt(CantGazapo.getText().toString()), "0", "0", "0", "0", 0, Comentario.getText().toString(),idPersonal, s.obtenerFechaActual(), per.getIdPersonal(), 0, "0");
	    	return Msg;

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Alert(result);
		}
	
	}
	
	public void Alert(String Msj){
		Toast.makeText(getApplicationContext(), Msj, Toast.LENGTH_SHORT).show();
	}
}
